package cn.tjie.org.service.impl;

import cn.tjie.basic.service.impl.BaseServiceImpl;
import cn.tjie.basic.util.Md5Utils;
import cn.tjie.basic.util.PageList;
import cn.tjie.basic.util.StrUtils;
import cn.tjie.org.domain.Department;
import cn.tjie.org.domain.Employee;
import cn.tjie.org.mapper.EmployeeMapper;
import cn.tjie.org.query.EmployeeQuery;
import cn.tjie.org.service.EmployeeService;
import cn.tjie.user.domain.Logininfo;
import cn.tjie.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务层的实现类：部门
 */
@Service//service 创建实例
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void save(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);//获取到自增ID

        //添加user的数据
        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);

        //添加到t_employee_role表，需要employee_id和role_id
        //employee_id employee添加完的到的自增长id就是它
        if(employee.getRole_id()!=null){

            employeeMapper.addRole(employee);
        }

    }

    @Override
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);

        //查以前的老数据：employee里面大的数据来自于前端  -就不会有logininfo_id,shop_id
        Employee oldEmployee = employeeMapper.loadById(employee.getId());//loginfo_id

        //修改user信息
        employee.setLogininfo_id(oldEmployee.getLogininfo_id());
        employee.setShop_id(oldEmployee.getShop_id());
        super.update(employee);
        //修改登陆信息
        Logininfo oldLogininfo = logininfoMapper.loadById(oldEmployee.getLogininfo_id());
        BeanUtils.copyProperties(employee,oldLogininfo);
        oldLogininfo.setId(oldEmployee.getLogininfo_id());
        logininfoMapper.update(oldLogininfo);

        //修改中间表 先删除再添加
        //删除中间表相关数据
        employeeMapper.delRoleById(employee.getId());
        //添加到t_employee_role表，需要employee_id和role_id
        //employee_id employee添加完的到的自增长id就是它
        if(employee.getRole_id()!=null){

            employeeMapper.addRole(employee);
        }
    }

    @Override
    public void del(Long id) {
        //先查询登陆信息id
        Employee employee = employeeMapper.loadById(id);
        //删除logininfo
        logininfoMapper.del(employee.getLogininfo_id());

        //删除中间表相关数据
        employeeMapper.delRoleById(employee.getId());
        //删除employee
        super.del(id);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }


    /**
     * 根据店铺id查询该店铺所有员工
     * @param id
     * @return
     */

    @Override
    public List<Employee> handleAccept(Long id) {
        return employeeMapper.handleAccept(id);
    }
}
